package org.dyndns.freed0m.web.secureauth.Utils;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sessionFactory = null; 
	private static final ThreadLocal<Session> depot = new ThreadLocal<Session>(); 

	static
	{
		try 
		{
			sessionFactory=new Configuration().configure().buildSessionFactory();
		}
		catch (HibernateException he)
		{
			he.printStackTrace();
		}
	}
	
	public static Session getCurrentSession() throws HibernateException
	{
		Session s = depot.get();
		if (s == null)
		{
			s = sessionFactory.openSession();
			depot.set(s);
		}
		return s;
	}
	
	public static void closeSession() throws HibernateException
	{
		Session s = depot.get();
		if (s != null)
		{
			s.close();
		}
		depot.remove();
	}
}
